<template>
  <BasicModal
    v-bind="$attrs"
    width="40%"
    :height="400"
    :title="title"
    @register="register"
    :loading="loading"
    loading-tip="加载中"
    @ok="submit"
  >
    <BasicForm @register="registerForm" />
    <template #footer>
      <div style="text-align: center">
        <a-button style="align-items: center" type="primary" @click="submit">
          提交
        </a-button>
      </div>
    </template>
  </BasicModal>
</template>
<script setup>
import { ref } from 'vue';
import { BasicModal, useModalInner } from '@/components/Modal';
import { useMessage } from '@/hooks/web/useMessage';
import { defHttp } from '@/utils/http/axios';
import { BasicForm, useForm } from '@/components/Form';
import { companyFormSchema } from '@/views/admin/company/data';
const { createMessage } = useMessage();
// const propsData = ref();
const loading = ref(true);
const title = ref();
const isEdit = ref();
const [register, { closeModal }] = useModalInner((data) => {
  resetFields();
  isEdit.value = data.isUpdate;
  if (data.isUpdate) {
    title.value = '编辑企业';
    getFormData(data.companyId);
  } else {
    title.value = '添加企业';
    loading.value = false;
  }
});
const [registerForm, { resetFields, getForm, setFieldsValue, validate }] =
  useForm({
    labelWidth: 125,
    schemas: companyFormSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
    // mode: 'edit',
  });
const emit = defineEmits(['success']);
// 确定提交
async function submit() {
  const formData = await validate();
  if (!isEdit.value) {
    const res = await defHttp.post({
      url: '/admin/company/save',
      data: JSON.stringify(formData),
    });
    closeModal();
    emit('success');
    createMessage.success('添加成功');
  } else {
    const res = await defHttp.post({
      url: '/admin/company/update',
      data: JSON.stringify(formData),
    });
    closeModal();
    emit('success');
    createMessage.success('修改成功');
  }
}
async function getFormData(id) {
  debugger;
  const res = await defHttp.get({
    url: '/admin/company/edit?id=' + id,
  });
  if (res) {
    await setFieldsValue({
      ...res.company,
      username: res.companyAdmin.username,
      adminName: res.companyAdmin.name,
      adminMobile: res.companyAdmin.mobile,
    });
    loading.value = false;
  }
}
const handleResizeColumn = (w, col) => {
  col.width = w;
};
</script>
